package com.landiao.ctrler.management;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.utils.CodecUtils;
import com.landiao.common.weixin.WxConsts;
import com.landiao.common.weixin.WxMpService;
import com.landiao.common.weixin.bean.WxMpMaterialUploadResult;
import com.landiao.ctrler.BaseCRUDCtrler;
import com.landiao.entities.BackstageUser;
import com.landiao.entities.Material;
import com.landiao.service.MaterialService;

/**
 * 功能说明:系统配置
 *
 */
@Controller
@RequestMapping("/management/material")
public class MaterialCtrler extends BaseCRUDCtrler<Material, String> {
	
	private MaterialService getMaterialService() {
        return (MaterialService) baseService;
    }
	
	
	@Override
	public String save(Material t, RedirectAttributes redirectAttr) {
		try{
			t.setCompId(this.getClientUser().getUser().getCompId());
			t.setMaterialId(CodecUtils.getUUID());
			t.setImg(this.getphoto(t.getImg()));
			String url = this.getRealUrl(t.getImg());
			getMaterialService().save(t,url);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("添加成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("添加失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("添加失败"));
		}
		return redirectToUrl(viewName("index"));
	}
	/**
	 * 验证生日关怀类型否存在
	 * 
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String purpose,String id) {
		if(purpose.equals("KQ")){
			return true;
		}
		Material material = new Material();
		material.setCompId(this.getClientUser().getUser().getCompId());
		material.setPurpose(purpose);
		Material result = this.getMaterialService().getByPurpose(material);
		if (result == null) {
			return true;
		}else{
			if(result.getMaterialId().equals(id)){
				return true;
			}else{
				return false;
			}
		}
	}
	@RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
	public String detail(Model model,@PathVariable String id) {
		Material t = getMaterialService().getById(id);
		model.addAttribute("t",t);
		return viewName("detail");
	}

	@Override
	public String update(Material t, RedirectAttributes redirectAttr) {
		try{
			String url = null;
			//判断图片是否修改
			if(this.isEditUrl(t.getImg())){
				url = this.getRealUrl(t.getImg());
			}
			t.setImg(this.getphoto(t.getImg()));
			getMaterialService().update(t,url);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("修改成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("修改失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("修改失败"));
		}
		return redirectToUrl(viewName("index"));
	}


	@ResponseBody
	@RequestMapping("listMaterial")
	public List<Material> listMaterial(Material material){
		material.setCompId(this.getClientUser().getUser().getCompId());
		List<Material> materials = this.getMaterialService().getAllMaterialList(material);
		return materials;
	}
	
	
}
